/**
 * 
 */
package com.dragonfly.shutter.controllers;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.DAOs.PictureDAO;
import com.dragonfly.shutter.DAOs.UserDAO;
import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.beans.PagedBean;
import com.dragonfly.shutter.beans.PictureDetail;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 审核图片的控制器
 * @author 赵洁琼
 *
 */
@Controller
public class checkPictureController {
	
	@Autowired
	private PictureDAO  pictureDAO;
	@Autowired
	private UserDAO userDAO;
	private static int pageSize = 1; //每页显示的数据数量
	private static int punishScore = 20;	//惩罚分数
	private static String illegalPicNotice = "illegalPicNotice.jpg";
		
	@RequestMapping(value ="/checkPicture.html", method = RequestMethod.GET)
	public ModelAndView setupCheckPictureForm(ModelMap model,HttpSession session,@RequestParam(value="page", required=false) String pageStr){
		Message message = null;
		
		Integer adminID = (Integer) session.getAttribute("adminID");
		
		if(adminID == null ){
			message = new Message("您尚未登录，请先登录", "请登录", "adminLogin.html");
			return new ModelAndView("message", "message", message);
		}
		
		if( pageStr == null){
			pageStr="1";
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pageStr)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		int page=Integer.parseInt(pageStr);
		PagedBean pagedBean = new PagedBean();
		pagedBean.setPageNum(page);
		pagedBean.setPageSize(pageSize);
		
		int minRow = ( page - 1 ) * pageSize + 1;
		int maxRow = page * pageSize; 
		pagedBean.setPagedItem(pictureDAO.selectReportedPicture(minRow, maxRow));
		pagedBean.setItemNum(pictureDAO.selectReportedPictureCount());
		
		int maxPage = (int) java.lang.Math.ceil( (double)pagedBean.getItemNum() / (double)pageSize );
		pagedBean.setMaxPage(maxPage);
		
		PagedBean pictureList=pagedBean;
		return new ModelAndView("checkPicture","pictureList",pictureList);
	}
	
	@RequestMapping(value ="/checkPicture.html", method = RequestMethod.POST)
	public ModelAndView checkPicture(@RequestParam(value="pictureID",required=true)String pictureID,
			@RequestParam(value="check",required=true)String check){
		
		if(check.equalsIgnoreCase("pass")){
			pictureDAO.updatePictureCheckFlag(pictureID, "pass");
		}
		else if(check.equalsIgnoreCase("notpass")){
			pictureDAO.updatePictureCheckFlag(pictureID, "notpass");
			//修改图片的可删除标记位
			PictureDetail picDetail = new PictureDetail();
			picDetail.setPictureID(pictureID);
			picDetail.setFlagDelete("Yes");
			pictureDAO.updatePictureDelFlag(picDetail);
			
			//告知收藏该图片的用户图片已经被删除，将图片的路径修改成通知图片的地址
			pictureDAO.updatePicLocation(pictureID, illegalPicNotice);

			//惩罚上传该图片的用户，更新用户分数
			Integer userID = pictureDAO.selectPicOwnerId(pictureID);
			Integer userPoint = userDAO.selectUserPoint(userID);
			userPoint -= punishScore;
			
			if(userPoint < 0 )
				userPoint = 0;
			
			userDAO.updateScore(userID, userPoint);
		}
		else{
			
		}
		
		return new ModelAndView("checkPicture");
	}
	
		
}

